using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MaterialChange : MonoBehaviour
{

    private List<Material> materials = new List<Material>();

    public GameObject obj;
    public Material shader;

    Material[] rendArray;
    Material[] mats;
    Material[] SaveMaterial;
    private void Start()
    {

        GetModelAllMaterialsAndChange(obj, shader);
    }


    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.A))
        {
            gameObject.transform.GetComponent<MeshRenderer>().materials = SaveMaterial;
        }
        if(Input.GetKeyDown(KeyCode.D))
        {
            gameObject.transform.GetComponent<MeshRenderer>().materials = mats;
        }
    }

    private void GetModelAllMaterialsAndChange(GameObject gameObject, Material targetShader)
    {
        rendArray = gameObject.transform.GetComponent<MeshRenderer>().materials;
        mats = new Material[rendArray.Length];
        SaveMaterial = rendArray;
        for (int i = 0; i < rendArray.Length; i++)
        {
            mats[i] = targetShader;
        }
        gameObject.transform.GetComponent<MeshRenderer>().materials = mats;
    }






    //public Material mat;
    //public GameObject go;
    ////public Button bt;
    //void Start()
    //{
    //    // bt.onClick.AddListener(ChangeShaderTwo);
    //    go.GetComponent<MeshRenderer>().material = mat;
    //}

    //private void Update()
    //{
    //    if (Input.GetKeyDown(KeyCode.A))
    //    {
    //        ChangeShaderTwo();
    //    }
    //}

    //public void ChangeShader()
    //{
    //    go.GetComponent<MeshRenderer>().material = mat;
    //}
    //public void ChangeShaderTwo()
    //{
    //    Material material = new Material(Shader.Find("Legacy Shaders/Transparent/Diffuse"));
    //    material.color = new Color(0, 1, 0, 0.3f);
    //    GetComponent<Renderer>().material = material;
    //}

}
